package com.droowa.controller;

import java.io.File;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.droowa.domain.BusinessRecord;
import com.droowa.domain.CompletePC;
import com.droowa.domain.CompletePCResult;
import com.droowa.domain.FirstCategory;
import com.droowa.domain.Product;
import com.droowa.domain.ShopManager;
import com.droowa.model.service.BusinessRecordService;
import com.droowa.model.service.ProductManagementService;

@Controller
@RequestMapping("/product/")
public class ProductController {
	
	@Autowired
	private ProductManagementService service;
	@Autowired
	private BusinessRecordService bRService;			//업무기록 서비스
	
	/****************************상품명으로 상품검색*********************************/

	@RequestMapping(value="searchProdctByName.do")
	public ModelAndView searchProductByName(String name, HttpSession session, HttpServletRequest request, @RequestParam(defaultValue="1") int page) throws SQLException{
		request.setAttribute("menu_category", "product");
		request.setAttribute("flag", "byName");
		request.setAttribute("name", name);
		Map map = service.getProductByName(name, page);
		session.setAttribute("result", map);
		if(map.get("product_list")==null){
			return new ModelAndView("admin.product/product_list","no_result", "검색 결과가 없습니다.");
		}
		return new ModelAndView("admin.product/product_list",map);
	}
		
	@RequestMapping("searchRProdctByName.do")
	public ModelAndView searchRProductByName(String name, HttpServletRequest request, HttpSession session, @RequestParam(defaultValue="1") int page) throws SQLException{
		request.setAttribute("menu_category", "product");
		request.setAttribute("flag", "byName");
		request.setAttribute("name", name);
		Map map = service.getRProductByName(name, page);
		session.setAttribute("result", map);
		if(map.get("removed_product_list")==null){
			return new ModelAndView("admin.product/removed_product_list","no_result", "검색 결과가 없습니다.");
		}
		return new ModelAndView("admin.product/removed_product_list", map);
	}
	
	/****************************카테고리명으로 상품검색*********************************/
	@RequestMapping(value="searchProdctByCategory.do")
	public ModelAndView searchProductByCategory(String categoryName, HttpSession session, HttpServletRequest request, @RequestParam(defaultValue="1") int page) throws SQLException{
		request.setAttribute("menu_category", "product");
		request.setAttribute("categoryName", categoryName);
		request.setAttribute("flag", "byCategory");
		Map map = service.getProductByCategoryName(categoryName, page);
		session.setAttribute("result", map);
		if(map.get("product_list")==null){
			return new ModelAndView("admin.product/product_list","no_result", "검색 결과가 없습니다.");
		}
		return new ModelAndView("admin.product/product_list", map);
	}
	
	@RequestMapping(value="searchRProdctByCategory.do")
	public ModelAndView searchRProductByCategory(String categoryName, HttpServletRequest request, HttpSession session, @RequestParam(defaultValue="1") int page) throws SQLException{
		request.setAttribute("menu_category", "product");
		request.setAttribute("categoryName", categoryName);
		request.setAttribute("flag", "byCategory");
		Map map = service.getRProductByCategoryName(categoryName, page);
		session.setAttribute("result", map);
		if(map.get("removed_product_list")==null){
			return new ModelAndView("admin.product/removed_product_list","no_result", "검색 결과가 없습니다.");
		}
		return new ModelAndView("admin.product/removed_product_list", map);
	}
	/****************************제조사명으로 상품검색*********************************/
	@RequestMapping(value="searchProdctByBrand.do")
	public ModelAndView searchProductByBrand(String brand, HttpSession session, HttpServletRequest request, @RequestParam(defaultValue="1") int page) throws SQLException{
		request.setAttribute("menu_category", "product");
		request.setAttribute("brand", brand);
		request.setAttribute("flag", "byBrand");
		Map map = service.getProductByBrandPaging(brand, page);
		session.setAttribute("result", map);
		if(map.get("product_list")==null){
			return new ModelAndView("admin.product/product_list","no_result", "검색 결과가 없습니다.");
		}
		return new ModelAndView("admin.product/product_list",map);
	}	
	@RequestMapping(value="searchRProdctByBrand.do")
	public ModelAndView searchRProductByBrand(String brand, HttpServletRequest request, HttpSession session, @RequestParam(defaultValue="1") int page) throws SQLException{
		request.setAttribute("menu_category", "product");
		request.setAttribute("menu_category", "product");
		request.setAttribute("brand", brand);
		request.setAttribute("flag", "byBrand");
		Map map = service.getRProductByBrandPaging(brand, page);
		session.setAttribute("result", map);
		if(map.get("removed_product_list")==null){
			return new ModelAndView("admin.product/removed_product_list","no_result", "검색 결과가 없습니다.");
		}
		return new ModelAndView("admin.product/removed_product_list", map);
	}
	
	/****************************모델명으로 상품검색*********************************/
	@RequestMapping(value="searchProdctByModelNo.do")
	public ModelAndView searchProductByModelNo(String modelNo, HttpSession session, HttpServletRequest request, @RequestParam(defaultValue="1") int page) throws SQLException{
		request.setAttribute("menu_category", "product");
		request.setAttribute("modelNo", modelNo);
		request.setAttribute("flag", "byModelNo");
		Map map = service.getProductByModelNo(modelNo, page);
		session.setAttribute("result", map);
		if(map.get("product_list")==null){
		return new ModelAndView("admin.product/product_list","no_result", "검색 결과가 없습니다.");
		}
	return new ModelAndView("admin.product/product_list", map);
}

	@RequestMapping(value="searchRProdctByModelNo.do")
	public ModelAndView searchRProductByModelNo(String modelNo, HttpServletRequest request, HttpSession session, @RequestParam(defaultValue="1") int page) throws SQLException{
		request.setAttribute("menu_category", "product");
		request.setAttribute("modelNo", modelNo);
		request.setAttribute("flag", "byModelNo");
		Map map = service.getRProductByModelNo(modelNo, page);
		session.setAttribute("result", map);
		if(map.get("removed_product_list")==null){
			return new ModelAndView("admin.product/removed_product_list","no_result", "검색 결과가 없습니다.");
		}
		return new ModelAndView("admin.product/removed_product_list",map);
	}
	
	/****************************가격 범위로 상품검색*********************************/
	@RequestMapping(value="searchProdctByPriceRange.do")
	public ModelAndView searchProductByPriceRange(int sPrice, int ePrice, HttpSession session, HttpServletRequest request, @RequestParam(defaultValue="1") int page) throws SQLException{
		request.setAttribute("menu_category", "product");
		request.setAttribute("sPrice", sPrice);
		request.setAttribute("ePrice", ePrice);
		request.setAttribute("flag", "byPriceRange");
		Map map = service.getProductByPriceRange(sPrice, ePrice, page);
		session.setAttribute("result", map);
		if(map.size()==0){
			return new ModelAndView("admin.product/product_list","no_result", "검색 결과가 없습니다.");
		}
		return new ModelAndView("admin.product/product_list", map);
	}

	@RequestMapping(value="searchRProdctByPriceRange.do")
	public ModelAndView searchRProductByPriceRange(int sPrice, int ePrice, HttpServletRequest request, HttpSession session, @RequestParam(defaultValue="1") int page) throws SQLException{
		request.setAttribute("menu_category", "product");
		request.setAttribute("sPrice", sPrice);
		request.setAttribute("ePrice", ePrice);
		request.setAttribute("flag", "byPriceRange");
		Map map = service.getRProductByPriceRange(sPrice, ePrice, page);
		session.setAttribute("result", map);
		if(map.size()==0){
			return new ModelAndView("admin.product/removed_product_list","no_result", "검색 결과가 없습니다.");
		}
		return new ModelAndView("admin.product/removed_product_list",map);
	}
	
	@RequestMapping("lowPriceSearch.do")
	public ModelAndView lowPriceSearch() throws SQLException{
		System.out.println("lowPrice");
		List list = service.getCompletePCByPrice(10, 300000);//매개변수 1 : StartPrice, 2: EndPrice
		if(list.size()==0){
			return new ModelAndView("customer.getProduct","no_result", "검색 결과가 없습니다.");
		}
		return new ModelAndView("customer.getProduct", "list", list);
	}
	@RequestMapping("middlePriceSearch.do")
	public ModelAndView middlePriceSearch() throws SQLException{
		List list = service.getCompletePCByPrice(300001, 500000);//매개변수 1 : StartPrice, 2: EndPrice
		if(list.size()==0){
			return new ModelAndView("customer.getProduct","no_result", "검색 결과가 없습니다.");
		}
		return new ModelAndView("customer.getProduct", "list", list);
	}
	@RequestMapping("highPriceSearch.do")
	public ModelAndView highPriceSearch() throws SQLException{
		List list = service.getCompletePCByPrice(500001, 1000000000);//매개변수 1 : StartPrice, 2: EndPrice
		if(list.size()==0){
			return new ModelAndView("customer.getProduct","no_result", "검색 결과가 없습니다.");
		}
		return new ModelAndView("customer.getProduct", "list", list);
	}
	
	@RequestMapping("cpuIntelSearch.do")
	public ModelAndView cpuIntelSearch() throws SQLException{
		String brand = "intel";
		List<Product> list = service.getProductByBrand(brand);
		if(list.size()==0){
			return new ModelAndView("customer.getProduct","no_result", "검색 결과가 없습니다.");
		}
		return new ModelAndView("customer.getProduct", "list", list);
	}
	
	@RequestMapping("cpuAmdSearch.do")
	public ModelAndView cpuAmdSearch() throws SQLException{
		String brand = "amd";
		List<Product> list = service.getProductByBrand(brand);
		if(list.size()==0){
			return new ModelAndView("customer.getProduct","no_result", "검색 결과가 없습니다.");
		}
		return new ModelAndView("customer.getProduct", "list", list);
	}
	
	@RequestMapping("getAllProduct.do")
	public String getAllProduct(HttpServletRequest request, @RequestParam(defaultValue="1") int page) throws SQLException{
		List<Product> list = service.getProductList();
		request.setAttribute("tabIndex", request.getParameter("tabIndex"));
		request.setAttribute("result1", service.getCompletePCByLow(page));
		request.setAttribute("result2", service.getCompletePCByHigh(page));
		request.setAttribute("result3", service.getCompletePCByCPUPaging(page));
		request.setAttribute("result4", service.getCompletePCByGRAPHICPaging(page));
		return "customer.getAllProduct";
	}
	
	@RequestMapping("getMagicGuide.do")
	public String getMagicGuide(HttpServletRequest request){
		//service 호출해서 매직가이드 조건에 맞는 product조회해서 스코프에 바인딩
		return "customer.magicGuide";
	}
	
	@RequestMapping("getProductInfo.do")
	public String getProductInfo(ModelMap map, String id, String sort, HttpServletRequest request) throws SQLException{
		Product product = service.getProductById(id);
		System.out.println(id);
		int price = product.getPrice();
		String category = product.getCategoryName();
		String name = product.getName();
		String fileName = product.getFileName();
		String completeId =product.getId();
		map.put("price", price);
		map.put("category", category);
		map.put("name", name);
		map.put("fileName", fileName);
		map.put("completeId", completeId);
		map.put("id", id);
		return "jsonView";
	}
	
	@RequestMapping("getCompletePCByCPUBrand.do")
	public ModelAndView getCompletePCByCPUBrand(String brand) throws SQLException{
		System.out.println("------------"+brand);
		List list = service.getCompletePCByCPUBrand("CPU", brand);
		if(list.size()==0){
			return new ModelAndView("customer.getProduct","no_result", "검색 결과가 없습니다.");
		}
		return new ModelAndView("customer.getProduct", "list", list);
	}
	/****************************카테고리 등록 페이지로 이동, 생성된 전체 리스트도 조회*********************************/
	@RequestMapping("categoryList.do")
	public ModelAndView firstCategoryList(HttpServletRequest request) throws Exception{
		request.setAttribute("menu_category", "product");
		List<FirstCategory> fixedList = service.getFixedCategoryList();
		request.setAttribute("fixedList", fixedList);
		List<FirstCategory> unFixedList = service.getUnFixedCategoryList();
		return new ModelAndView("admin.product/register_category","unFixedList", unFixedList);
	}
	/****************************카테고리 등록 *********************************/
	@RequestMapping("firstCategory.do")
	public ModelAndView registerFirstCategory(FirstCategory firstCategory, HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception{
		request.setAttribute("menu_category", "product");
		List<FirstCategory> fixedList = service.getFixedCategoryList();
		request.setAttribute("fixedList", fixedList);
		String name = firstCategory.getCategoryName();
		List<FirstCategory> unFixedList = service.getUnFixedCategoryList();
		for(FirstCategory fc : fixedList){
			if(name.equals(fc.getCategoryName())){
				request.setAttribute("error_message", "중복된 카테고리 명입니다");
				return new ModelAndView("admin.product/register_category","unFixedList", unFixedList);
			}
		}
		for(FirstCategory fc : unFixedList){
			if(name.equals(fc.getCategoryName())){
				request.setAttribute("error_message", "중복된 카테고리 명입니다");
				return new ModelAndView("admin.product/register_category","unFixedList", unFixedList);
			}
		}
		service.registerFirstCategory(firstCategory);			
		insertBusinessRecord("상품", "카테고리등록", session, firstCategory);
		unFixedList = service.getUnFixedCategoryList();
		return new ModelAndView("admin.product/register_category","unFixedList", unFixedList);				
	}
	
	/****************************카테고리 삭제 *********************************/
	@RequestMapping("removeCategory.do")
	public ModelAndView removeFirstCategoryById(String categoryId, HttpServletRequest request, HttpSession session) throws Exception{
		request.setAttribute("menu_category", "product");
		service.removeFirstCategoryById(categoryId);
		insertBusinessRecord("상품", "카테고리삭제", session, categoryId);
		List<FirstCategory> fixedList = service.getFixedCategoryList();
		request.setAttribute("fixedList", fixedList);
		List<FirstCategory> unFixedList = service.getUnFixedCategoryList();
		return new ModelAndView("admin.product/register_category","unFixedList", unFixedList);
	}
	/****************************카테고리 수정 페이지로 이동 *********************************/
	@RequestMapping("getCategoryId.do")
	public ModelAndView getFirstCategoryById(String categoryId, FirstCategory firstCategory, HttpServletRequest request) throws Exception{
		request.setAttribute("menu_category", "product");
		firstCategory = service.getFirstCategoryById(categoryId);
		return new ModelAndView("admin.product/modify_category", "firstCategory", firstCategory);
	}
	
	
	/****************************카테고리 수정 후 카테고리 페이지로 이동 *********************************/
	@RequestMapping(value="modifyCategory.do", method=RequestMethod.POST )
	public ModelAndView updateFirstCategoryById(FirstCategory firstCategory, Product product, HttpServletRequest request, HttpSession session)throws Exception{
		request.setAttribute("menu_category", "product");
		
		List<FirstCategory> fixedList = service.getFixedCategoryList();
		request.setAttribute("fixedList", fixedList);
		
		String name = firstCategory.getCategoryName();
		List<FirstCategory> unFixedList = service.getUnFixedCategoryList();
		ModelAndView mav = new ModelAndView("admin.product/modify_category", "firstCategory", firstCategory);
		for(FirstCategory fc : fixedList){
			if(name.equals(fc.getCategoryName())){
				request.setAttribute("error_message", "중복된 카테고리 명입니다");
				return mav;
			}
		}
		for(FirstCategory fc : unFixedList){
			if(name.equals(fc.getCategoryName())){
				request.setAttribute("error_message", "중복된 카테고리 명입니다");
				return mav;
			}
		}
		service.modifyFirstCategoryById(firstCategory);
		service.modifyProductCategoryName(product);
		insertBusinessRecord("상품", "카테고리수정", session, firstCategory);
		fixedList = service.getFixedCategoryList();
		request.setAttribute("fixedList", fixedList);
		unFixedList = service.getUnFixedCategoryList();
		return new ModelAndView("admin.product/register_category","unFixedList", unFixedList);
	}
	/****************************상품등록 페이지로 이동*********************************/
	@RequestMapping("product.do")
	public ModelAndView productForm(HttpServletRequest request)throws Exception{
		request.setAttribute("menu_category", "product");
		List<FirstCategory> list = service.getCategoryList();
		return new ModelAndView("admin.product/register_product","category_list", list);
	}
	
		/****************************상품조회 페이지로 이동*********************************/
	@RequestMapping("productList.do")
	public ModelAndView productList(HttpServletRequest request, HttpSession session, @RequestParam(defaultValue="1") int page) throws Exception{
		request.setAttribute("menu_category", "product");
		request.setAttribute("flag", "byList");
		List<Product> list = service.getProductList();
		session.setAttribute("result", list);
		Map map = service.getProductListPaging(page);
		return new ModelAndView("admin.product/product_list", map);
	}

	/****************************상품등록 후 조회 페이지로 이동*********************************/
	@RequestMapping(value="registerProduct.do", method=RequestMethod.POST )
	public ModelAndView registerProduct(Product product, HttpServletRequest request, @RequestParam("pic") MultipartFile pic, HttpSession session, @RequestParam(defaultValue="1") int page) throws Exception{
		request.setAttribute("menu_category", "product");
		request.setAttribute("flag", "byList");
		String fileName = pic.getOriginalFilename();
		String saveDir = "C:\\day\\apache-tomcat-6.0.41\\webapps\\droowa\\product-img";
		product.setFileName(fileName);
		service.registerProduct(product);
		insertBusinessRecord("상품", "상품등록", session, product);						// 업무기록
		if(pic!=null&&!pic.isEmpty()){
			pic.transferTo(new File(saveDir, fileName));
		}
		Map map = service.getProductListPaging(page);
		return new ModelAndView("admin.product/product_list", map);
	}
	
	/****************************상품 리스트에서삭제 후 조회 페이지로 이동*********************************/
	@RequestMapping("removeProduct.do")
	public String removeProductDel(Product product, HttpServletRequest request, HttpSession session, int page) throws Exception{
		request.setAttribute("menu_category", "product");
		request.setAttribute("flag", "byList");
		service.modifyDelToTrue(product);
		insertBusinessRecord("상품", "상품삭제", session, product);						//업무기록
		
		//Map map = service.getProductListPaging(page);
		//return new ModelAndView("admin.product/product_list", map);
        return "redirect:productList.do?page="+page;	
	}
	/****************************상품수정 페이지로 이동*********************************/
	@RequestMapping("getProductId.do")
	public ModelAndView getProductById(String id, HttpServletRequest request) throws Exception{
		request.setAttribute("menu_category", "product");
		Product product = service.getProductById(id);
		return new ModelAndView("admin.product/modify_product", "product", product);
	}
	/****************************상품수정 후 조회 페이지로 이동*********************************/
	@RequestMapping(value="modifyProduct.do", method=RequestMethod.POST )
	public ModelAndView updateProduct(Product product, HttpServletRequest request,  @RequestParam("pic") MultipartFile pic, HttpSession session, @RequestParam(defaultValue="1") int page)throws Exception{
		request.setAttribute("menu_category", "product");
		request.setAttribute("flag", "byList");
		String fileName = pic.getOriginalFilename();
		String saveDir = "C:\\day\\apache-tomcat-6.0.41\\webapps\\droowa\\product-img";
		String flag = request.getParameter("del");
		String picture = request.getParameter("picture");
		if(pic!=null&&!pic.isEmpty()){
			product.setFileName(fileName);
			service.modifyProduct(product);
			insertBusinessRecord("상품", "상품수정", session, product);						//업무기록
			pic.transferTo(new File(saveDir, fileName));
			Map map = service.getProductListPaging(page);
			Map rmap = service.getRemovedProductListPaging(page);
			if(flag.equals("false")){	
				return new ModelAndView("admin.product/product_list", map);
			}else{	
				return new ModelAndView("admin.product/removed_product_list",rmap);
			}
		}
		product.setFileName(picture);
		service.modifyProduct(product);
		insertBusinessRecord("상품", "상품수정", session, product);						//업무기록
		Map map = service.getProductListPaging(page);
		Map rmap = service.getRemovedProductListPaging(page);
		if(flag.equals("false")){	
			return new ModelAndView("admin.product/product_list", map);
		}else{	
			return new ModelAndView("admin.product/removed_product_list",rmap);
		}
	}
	/****************************삭제상품 조회 페이지로 이동*********************************/
	@RequestMapping("removedProductList.do")
	public ModelAndView removedProductList(HttpServletRequest request, @RequestParam(defaultValue="1") int page) throws Exception{
		request.setAttribute("menu_category", "product");
		request.setAttribute("flag", "byList");
		Map map = service.getRemovedProductListPaging(page);
		return new ModelAndView("admin.product/removed_product_list", map);
	}
	/****************************상품삭제 리스트에서 복구 후 조회 페이지로 이동*********************************/
	@RequestMapping("recoverProduct.do")
	public ModelAndView recoverProductDel(Product product, HttpServletRequest request, @RequestParam(defaultValue="1") int page) throws Exception{
		request.setAttribute("menu_category", "product");
		request.setAttribute("flag", "byList");
		service.modifyDelToFalse(product);
		Map map = service.getRemovedProductListPaging(page);
		return new ModelAndView("admin.product/removed_product_list", map);
	}
	/****************************상품완전삭제*********************************/
	@RequestMapping("finalRemoveProduct.do")
	public ModelAndView finalProductDel(String id, HttpServletRequest request, @RequestParam(defaultValue="1") int page) throws Exception{
		request.setAttribute("menu_category", "product");
		request.setAttribute("flag", "byList");
		service.removeProductById(id);
		Map map = service.getRemovedProductListPaging(page);
		return new ModelAndView("admin.product/removed_product_list", map);
	}
	/****************************재고 조회 페이지로 이동*********************************/
	@RequestMapping("inventory.do")
	public ModelAndView inventoryList(HttpServletRequest request, @RequestParam(defaultValue="1") int page)throws Exception{
		request.setAttribute("menu_category", "product");
		Map map = service.getInventoryList(page);
		return new ModelAndView("admin.product/inventory", map);
	}
	/****************************재고 제이슨 처리*********************************/
	@RequestMapping("getInvJSON.do")
	public String getInvJSON(HttpServletRequest request, ModelMap map)throws Exception{
		request.setAttribute("menu_category", "product");
		String id = request.getParameter("id");
		Product product = service.getProductById(id);
		String name = product.getName();
		map.addAttribute("name", name);
		return "jsonView";
	}
	
	/****************************안전재고 업데이트*********************************/
	@RequestMapping("updateSafeInventory.do")
	public ModelAndView updateSafeInventory(Product product, HttpServletRequest request, @RequestParam(defaultValue="1") int page) throws Exception{
		request.setAttribute("menu_category", "product");
		service.modifySafeInventory(product);
		Map map = service.getInventoryList(page);
		return new ModelAndView("admin.product/inventory", map);
	}
	
	/****************************조립페이지로 이동*********************************/
	@RequestMapping("assembling.do")
	public ModelAndView assembling(HttpServletRequest request, HttpSession session) throws Exception{
		request.setAttribute("menu_category", "product");
		List<Product> list = service.getProductList();
		session.setAttribute("result", list);
		return new ModelAndView("admin.product/assembling","product_list", list);
	}
	
	/****************************PC등록 후 이동*********************************/
	@RequestMapping(value="registerPC.do", method=RequestMethod.POST)
	public ModelAndView registerPC(HttpServletRequest request, HttpSession session, CompletePC completePC, @RequestParam("pic") MultipartFile pic, ModelMap map) throws Exception{
		request.setAttribute("menu_category", "product");
		String fileName = pic.getOriginalFilename();
		String saveDir = "C:\\day\\apache-tomcat-6.0.41\\webapps\\droowa\\product-img";
		completePC.setFileName(fileName);
		service.registerCompletePC(completePC);
		insertBusinessRecord("상품", "완제품등록", session, completePC);
		if(pic!=null&&!pic.isEmpty()){
			pic.transferTo(new File(saveDir, fileName));
		}
		List list = service.getCompletePCList();
		return new ModelAndView("admin.product/computer_list","computer_list", list);
	}
	
	/****************************PC 관리 페이지 이동*********************************/
	@RequestMapping("computerList.do")
	public ModelAndView computerList(HttpServletRequest request, HttpSession session, @RequestParam(defaultValue="1") int page) throws Exception{
		request.setAttribute("menu_category", "product");
		Map map = service.getCompletePCListPaging(page);
		session.setAttribute("result", map);
		return new ModelAndView("admin.product/computer_list", map);
	}
	
	/****************************PC 제이슨 처리*********************************/
	@RequestMapping("getCompleteJSON.do")
	public String getCompleteJSON(HttpServletRequest request, ModelMap map)throws Exception{
		request.setAttribute("menu_category", "product");
		String id = request.getParameter("id");
		List complete = service.getCompleteNameById(id);
		map.addAttribute("complete", complete);
		return "jsonView";
	}
	
	/****************************PC 수정 페이지 이동*********************************/
	@RequestMapping("getCompletePCById.do")
	public ModelAndView getCompletePCById(HttpServletRequest request, String completeId) throws Exception{
		Map priceMap = service.getCompletePCByIdPrice(completeId);
		request.setAttribute("completePCDetailPrice", priceMap);
		request.setAttribute("menu_category", "product");
		request.setAttribute("product_list", service.getProductList());
		request.setAttribute("complete",service.getCompleteDomain(completeId));
		List completePC = service.getCompletePC(completeId);
		return new ModelAndView("admin.product/modify_computer","completePC", completePC);
	}
	
	/****************************PC 수정후 관리페이지 이동*********************************/
	@RequestMapping("updateCompletePC.do")
	public ModelAndView updateCompletePC(HttpServletRequest request, HttpSession session, CompletePC completePC, @RequestParam("pic") MultipartFile pic, @RequestParam(defaultValue="1") int page) throws Exception{
		request.setAttribute("menu_category", "product");
		String fileName = pic.getOriginalFilename();
		String saveDir = "C:\\day\\apache-tomcat-6.0.41\\webapps\\droowa\\product-img";
		String picture = request.getParameter("picture");
		if(pic!=null&&!pic.isEmpty()){
			completePC.setFileName(fileName);
			service.modifyCompletePC(completePC);
			insertBusinessRecord("상품", "완제품수정", session, completePC);
			pic.transferTo(new File(saveDir, fileName));
			Map map = service.getCompletePCListPaging(page);
			return new ModelAndView("admin.product/computer_list", map);
		}
		completePC.setFileName(picture);
		service.modifyCompletePC(completePC);
		insertBusinessRecord("상품", "완제품수정", session, completePC);
		Map map = service.getCompletePCListPaging(page);
		return new ModelAndView("admin.product/computer_list", map);	
}
	
	/****************************PC 삭제 *********************************/
	@RequestMapping("removeCompletePC.do")
	public ModelAndView removeCompletePCById(String completeId, HttpServletRequest request, HttpSession session, @RequestParam(defaultValue="1") int page) throws Exception{
		request.setAttribute("menu_category", "product");
		service.removeCompletePCById(completeId);
		insertBusinessRecord("상품", "완제품삭제", session, completeId);
		Map map = service.getCompletePCListPaging(page);
		return new ModelAndView("admin.product/computer_list", map);
	}
	
	/****************************이벤트 설정 *********************************/
	@RequestMapping("setEvent.do")
	public ModelAndView modifyEventToTrue(CompletePC completePC, HttpSession session, HttpServletRequest request, @RequestParam(defaultValue="1") int page) throws Exception{
		request.setAttribute("menu_category", "product");
		service.modifyEventToTrue(completePC);
		insertBusinessRecord("상품", "이벤트상품등록", session, completePC);
		Map map = service.getCompletePCListPaging(page);
		return new ModelAndView("admin.product/computer_list",map);
	}
	
	@RequestMapping("releaseEvent.do")
	public ModelAndView modifyEventToFalse(CompletePC completePC, HttpSession session, HttpServletRequest request, @RequestParam(defaultValue="1") int page) throws Exception{
		request.setAttribute("menu_category", "product");
		service.modifyEventToFalse(completePC);
		insertBusinessRecord("상품", "이벤트상품해제", session, completePC);
		Map map = service.getCompleteEventPaging(page);
		return new ModelAndView("admin.product/event",map);
	}
	
	/****************************이벤트페이지로 이동*********************************/
	@RequestMapping("registerEvent.do")
	public ModelAndView registerEvent(CompletePC completePC, HttpServletRequest request, @RequestParam(defaultValue="1") int page) throws Exception{
		request.setAttribute("menu_category", "product");
		Map map = service.getCompleteEventPaging(page);
		return new ModelAndView("admin.product/event", map);
	}
	
	/****************************Customer 이벤트 페이지**************************/
	@RequestMapping("getEventProduct.do")
	public ModelAndView getEventProduct(CompletePC completePC, HttpServletRequest request) throws Exception{
		List<CompletePC> list = service.getEventList();
		return new ModelAndView("customer.getEventProduct", "event_list", list);
	}
	
	/**************************** 제품상세 페이지로 이동 *********************************/
	@RequestMapping(value="detailProduct.do", method=RequestMethod.GET)
	public String detailProduct(HttpServletRequest request, String completeId, HttpSession session) throws SQLException{
		Map<String , String> map=service.getCompletePCById(completeId);
		List<Product> list = service.getProductList();
		Map priceMap = service.getCompletePCByIdPrice(completeId);
		CompletePC completePCBasic = service.getCompletePCByIdBasic(completeId);
		request.setAttribute("product_list", list);
		request.setAttribute("completeId", completeId);
		request.setAttribute("pc", map);
		request.setAttribute("completePCDetailPrice", priceMap);
		request.setAttribute("completePCBasic", completePCBasic);
		session.setAttribute("completePCBasic", completePCBasic);
		System.out.println(completePCBasic);
		
		return "customer.detailProduct";
	}
	
	/****************************BusinessRecord 등록*****************************/
	private void insertBusinessRecord(String sort, String sort2, HttpSession session, Object instance) throws SQLException {
		ShopManager sm = (ShopManager)session.getAttribute("login_info_manager");
		if(instance.getClass().getName().equals("com.droowa.domain.Product")){
			Product product = (Product)instance;
			bRService.joinBusinessRecord(new BusinessRecord(null, sort, sort2, new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()), sm.getSort()+" ("+product.getId()+") "));
		}else if(instance.getClass().getName().equals("com.droowa.domain.CompletePC")){
			CompletePC pc = (CompletePC)instance;
			bRService.joinBusinessRecord(new BusinessRecord(null, sort, sort2, new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()), sm.getSort()+" ("+pc.getCompleteId()+") "));
		}else if(instance.getClass().getName().equals("com.droowa.domain.FirstCategory")){
			FirstCategory firstCategory = (FirstCategory)instance;
			bRService.joinBusinessRecord(new BusinessRecord(null, sort, sort2, new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()), sm.getSort()+" ("+firstCategory.getCategoryId()+") "));
		}else{
		}
		
	}

	private void insertBusinessRecord(String sort, String sort2, HttpSession session, String id) throws SQLException {
		ShopManager sm = (ShopManager)session.getAttribute("login_info_manager");
		bRService.joinBusinessRecord(new BusinessRecord(null, sort, sort2, new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()), sm.getSort()+" ("+id+") "));		
	}

	//=====================Excel 파일 생성=======================
	@RequestMapping("productView.do")
	public String productView(Map<String, Object> model, HttpSession session) throws SQLException{
		List<Product> list = (List<Product>)session.getAttribute("result");
		model.put("product", list);
		return "productView";
	}
	
	@RequestMapping("completePCView.do")
	public String completePCView(Map<String, Object> model, HttpSession session) throws SQLException{
		List<CompletePCResult> list = (List<CompletePCResult>)session.getAttribute("result");
		model.put("completePC", list);
		return "completePCView";
	}
	
}